<?php

require_once './application/core/Dashboard/DashboardCore.php';

class monitorCore extends DashboardCore
{

    private $_class;
    private $_html;
    private $_ip;
    private $_file;
    private $_name;
    private $_base;

    public function __construct(&$controller)
    {
        parent::__construct($controller);
        $this->_base = new CoreBase($controller);
        $this->_obj = new DashboardCore($controller);
    }

    public function ajouterMoniteur()
    {
        $this->_ip = $this->_controller->getRequest()->getParam('ip');
        $this->_name = $this->_controller->getRequest()->getParam('name');
        $this->_ip = $this->_base->parseArrayIp($this->_ip);
        $this->_ip = explode(",", $this->_ip);
        $this->_name = $this->_base->parseArrayIp($this->_name);
        $this->_name = explode(",", $this->_name);
        $i = 0;
        foreach ($this->_ip as $ip)
        {
            $this->creeClass($ip, $this->_name[$i]);
            $this->creeView($this->_name[$i]);
            $this->_obj->ajouterModule("Moniteur " . $this->_name[$i], "monitor_" . $this->_name[$i], "Moniteur " . $this->_name[$i], 0, "monitor.png", "Audit Discovery Network", "support@ad-network.eu");
            $i++;
        }
    }

    public function ajouterMoniteurAction($name, $ip)
    {
        $this->creeClass($ip, $name);
        $this->creeView($name);
        $this->ajouterModule("Moniteur " . $name, "monitor_" . $name, "Moniteur " . $name, 0, "monitor.png", "Audit Discovery Network", "support@ad-network.eu");
    }

    private function creeView($name)
    {
        $this->_location = APPLICATION_PATH . "/application/views/scripts/module/monitor_";
        $this->_file = fopen($this->_location . $name . ".phtml", 'w');
        $this->_html = "<div>
    <?php echo \$this->title; ?>
    <p>
        " . VISUALISATION_BODY_MONITOR . " : <?php echo \$this->statut;?>
    </p>
</div>";
        fwrite($this->_file, $this->_html);
        fclose($this->_file);
    }

    private function creeClass($ip, $name)
    {
        $this->_location = APPLICATION_PATH . "/application/core/Module/monitor_";
        $this->_file = fopen($this->_location . $name . ".php", 'w');
        $this->_class = "<?php
class monitor_". $name . "Module
{
    private \$_ip;
    private \$_rn;
    private \$_ch;

    private function init_ini()
    {
        \$this->_ip = \"" . $ip . "\";
         return \$this->_ip;
    }
    private function con_monitor()
    {
        \$this->_ch = curl_init();
        curl_setopt(\$this->_ch, CURLOPT_URL, \$this->_ip);
        curl_setopt(\$this->_ch,CURLOPT_CONNECTTIMEOUT,1);
        curl_setopt(\$this->_ch,CURLOPT_HEADER,true);
        curl_setopt(\$this->_ch,CURLOPT_NOBODY,true);
        curl_setopt(\$this->_ch,CURLOPT_RETURNTRANSFER,true);
        \$this->_rn = curl_exec(\$this->_ch);
        curl_close(\$this->_ch);
        if (\$this->_rn)
            return \"<span style='color: green;'>On</span>\";
        else
            return  \"<span style='color: red;'>Off</span><script>alertGrowl('Alerte', '" . VISUALISATION_BODY_MONITOR . " " . $name . " off')</script>\";
    }

    public static function execute()
    {
        \$obj = new monitor_". $name . "Module();
        \$ip = \$obj->init_ini();
        \$rn = \$obj->con_monitor(\$ip);
        return array( \"title\" => \"<b>Statut :</b>\",
                \"statut\" => \$rn );
    }
}
?>";
        fwrite($this->_file, $this->_class);
        fclose($this->_file);
    }

}